package com.tubeonfire.model.admin;

import java.util.Collections;
import java.util.logging.Logger;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.tubeonfire.entity.Advertising;

public class AdvertisingModel {

	private static final Logger log = Logger.getLogger(AdvertisingModel.class
			.getName());

	private static Objectify ofy;

	private static Cache cache = null;

	private static boolean isRegisted = false;

	private static ObjectifyOpts opts = null;

	private static String cachePrefix = "AdvertisingModel_";

	public static void init() {
		if (!isRegisted) {
			isRegisted = true;
			try {
				ObjectifyService.register(Advertising.class);
			} catch (Exception e) {
				isRegisted = false;
			}
			try {
				cache = CacheManager.getInstance().getCacheFactory()
						.createCache(Collections.emptyMap());
			} catch (CacheException e) {
				isRegisted = false;
			}
			opts = new ObjectifyOpts().setSessionCache(true);
		}
		ofy = ObjectifyService.begin(opts);
	}

	public AdvertisingModel() {
		init();
	}

	@SuppressWarnings("unchecked")
	public static void insert(Advertising obj) {
		init();
		if (cache != null) {
			String prefix = cachePrefix + "id_" + obj.getId();
			cache.put(prefix, obj);
			cache.put("frontEndAdvertisingModel_" + "id_" + obj.getId(), obj);
			System.out.println("Put Advertising to cache success !");
		}
		ofy.put(obj);
	}

	public static void delete(Advertising obj) {
		init();
		String prefix = cachePrefix + "id_" + obj.getId();
		if (cache != null && cache.containsKey(prefix)) {
			cache.remove(prefix);
			cache.remove("frontEndAdvertisingModel_" + "id_" + obj.getId());
			System.out.println("Remove ads from cache success !");
		}
		ofy.delete(obj);
	}

	@SuppressWarnings("unchecked")
	public static Advertising byId(String id, boolean fromCache) {
		init();
		Advertising obj = new Advertising();
		String prefix = cachePrefix + "id_" + id;
		if (cache != null && cache.containsKey(prefix) && fromCache) {
			obj = (Advertising) cache.get(prefix);
		} else {
			try {
				obj = ofy.get(new Key<Advertising>(Advertising.class, id));
				if (fromCache) {
					cache.put(prefix, obj);
				}
			} catch (Exception e) {
				obj = null;
			}
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	public static Advertising getByPlaceAndPosition(int place, int position) {
		try {
			init();
			Advertising obj = new Advertising();
			String prefix = cachePrefix + "id_" + "advertising_place_" + place
					+ "_position_" + position;
			if (cache != null && cache.containsKey(prefix)) {
				obj = (Advertising) cache.get(prefix);
			} else {
				obj = ofy.query(Advertising.class).filter("position", position)
						.filter("place", place).get();
				if (obj != null) {
					cache.put(prefix, obj);
				}
			}
			return obj;
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			return null;
		}
	}
}
